/**
 * 和普元同步数据
 * @author yanwl 
 * @date 2014-10-20
 */
package com.chnbst.bstyun.eos;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.booleansoft.model.redirect.Servers;
import com.booleansoft.redirect.client.ConnectClient;
import com.booleansoft.redirect.client.TomcatClient;
import com.booleansoft.util.NumberHelper;
import com.chnbst.bstyun.core.common.DESUtil;
import com.chnbst.bstyun.core.common.HttpRequest;
import com.chnbst.bstyun.core.common.OaConfig;
import com.chnbst.bstyun.core.common.ReadProperties;
import com.chnbst.bstyun.core.common.SignUtil;
import com.chnbst.bstyun.model.pojo.Goods;
import com.chnbst.bstyun.model.pojo.GoodsRelation;
import com.chnbst.bstyun.model.pojo.OperateApply;
import com.chnbst.bstyun.model.pojo.Order;
import com.chnbst.bstyun.model.pojo.temp.CallBill;
import com.chnbst.bstyun.model.vo.UserVo;
import com.chnbst.bstyun.service.GoodsRelationService;
import com.chnbst.bstyun.service.GoodsService;
import com.chnbst.bstyun.service.OperateApplyService;
import com.chnbst.bstyun.service.OrderService;
import com.chnbst.bstyun.service.UserService;
import com.chnbst.bstyun.task.Task;

@Component
public class Eos {
	@Autowired
	private OrderService orderService;
	@Autowired
	private UserService userService;
	@Autowired
	private ReadProperties read;
	@Autowired
	private Task task;
	@Autowired
	private OaConfig oa;
	@Autowired
	private GoodsRelationService goodsRelationService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private OperateApplyService operateApplyService;
	
	private final static Logger logger = LoggerFactory.getLogger(Eos.class);
	
	/**
	 * 购买成功后给普元同步套餐购买信息
	 * @param order 订单对象
	 */
	@SuppressWarnings("unchecked")
	public String sendMeal(Order order) throws Exception {
		UserVo user = userService.selectUserVoByUserId(order.getUserId());
		
		String goodsId = order.getGoodsId();
		List<GoodsRelation> goodsRelations = goodsRelationService.selectByGoodsId(goodsId);
		
		String appCode = "";
		for (GoodsRelation goodsRelation : goodsRelations) {
			appCode += goodsRelation.getProductNum()+",";
		}
		Goods goods = goodsService.selectByGoodsId(goodsId);
		
		Integer amount = order.getUserNum();
		String expires = SignUtil.time2str(order.getOverTime(), "yyyy-MM-dd");
		
		String industry = user.getCompanyIndustryName();
		String scale = user.getCompanyScaleName();
		String userId = user.getUserEmail();
		String password = DESUtil.decrypt(user.getUserPassword());
		String orgName = user.getBelongCompanyName();
		String empName = user.getContactName();
		String email = user.getBoundEmail();
		String[] str = user.getCompanyDomainName().split("www.");
		String domain = str[1];
		String deptName = user.getCompanyDepartment();
		String phone = user.getPhone();
		String combo = goods.getGoodsName();
		String version = goods.getGoodsVersion();

		String params = "userId="+userId+"&password="+password+"&orgName="+orgName
					  + "&empName="+empName+"&email="+email+"&industry="+industry+"&scale="+scale
					  + "&domain="+domain+"&deptName="+deptName+"&appCode="+appCode+"&amount="+amount
					  +"&expires="+expires+"&phone="+phone+"&combo="+combo+"&version="+version;
		long startTime = System.currentTimeMillis();
		String url = read.getZcktURL() + params;
		String friendUrl = read.getFirendURL()+"tenantid="+domain;
		logger.info("zcktURL:"+url);
		logger.info("friendUrl:"+friendUrl);
		logger.info("套餐购买开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String result = HttpRequest.sendHttpRequest(url,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		
		HttpRequest.sendHttpRequest(friendUrl,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		long endTime = System.currentTimeMillis();
		logger.info("套餐购买结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("套餐购买调用接口时间毫秒数："+(endTime-startTime));
		logger.info(result);
		 Map<String, Object> returnMap = new ObjectMapper().readValue(result, Map.class);
	     return (String) returnMap.get("isReturn");
	}
	
	/**
	 * 购买成功后给普元同步单个产品购买信息
	 * @param order 订单对象
	 */
	@SuppressWarnings("unchecked")
	public String sendProduct(Order order) throws Exception {
		UserVo user = userService.selectUserVoByUserId(order.getUserId());
		
		List<GoodsRelation> goodsRelations = goodsRelationService.selectByGoodsId(order.getGoodsId());
		String appCode = goodsRelations.get(0).getProductNum();
		
		int amount = order.getUserNum();
		String expires = SignUtil.time2str(order.getOverTime(), "yyyy-MM-dd");
		
		String[] str = user.getCompanyDomainName().split("www.");
		String domain = str[1];
		String params = "domain="+domain+"&appCode="+appCode+"&amount="+amount+"&expires="+expires;
		
		String portalUrl = read.getProtalURL()+ params;
		String defaultUrl = read.getDefaultURL()+ params;
		logger.info("portalUrl:"+portalUrl);
		logger.info("defaultUrl:"+defaultUrl);
		
		long startTime = System.currentTimeMillis();
		logger.info("增值服务购买开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));

		String isportal = HttpRequest.sendHttpRequest(portalUrl,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		String isdefault = HttpRequest.sendHttpRequest(defaultUrl,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		
		logger.info(isportal+"--"+isdefault);
		String result = "";
		String codeStr = "";
		if(isportal.contains("isReturn") || isportal.contains("exception") || isdefault.contains("isReturn") || isdefault.contains("exception")) {
			result = "数据同步请求异常";
		}else {
			Map<String, Object> returnMap1 = new ObjectMapper().readValue(isportal, Map.class);
			Map<String, Object> returnMap2 = new ObjectMapper().readValue(isdefault, Map.class);
			int res1 = (Integer) returnMap1.get("isportal");
			boolean res2 = (boolean) returnMap2.get("isdefault");
			String corpCode =  (String)returnMap2.get("corpCode");
			String deptCode =  (String)returnMap2.get("deptCode");
			
			if(res1 == 1 && res2) {
				result = "ok";
				codeStr = corpCode+";"+deptCode;
			}else {
				result = "exception";
			}
		}
		long endTime = System.currentTimeMillis();
		logger.info("增值服务购买结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("增值服务购买调用接口时间毫秒数："+(endTime-startTime));
		logger.info(result);
		
		if("ok".equalsIgnoreCase(result)) {
			logger.info("企业code和部门code是："+codeStr);
			int userCount = amount + 3;
			if("BST-OA001".equals(appCode) || "BST-YUNXT001".equals(appCode) || "BST-CMS001".equals(appCode)) {
				ktZZ(domain, user.getBelongCompanyName(), userCount, SignUtil.str2Date(expires, "yyyy-MM-dd"),codeStr,appCode);
			}
		}
		return result;
	}
	
	/**
	 * 升级或者续费后给普元同步产品购买信息
	 * @param order 订单对象
	 */
	@SuppressWarnings("unchecked")
	public String sendProOrMeal(Order order) throws Exception {
		UserVo user = userService.selectUserVoByUserId(order.getUserId());
		String goodsId = order.getGoodsId();
		Goods goods = goodsService.selectByGoodsId(goodsId);
		List<GoodsRelation> goodsRelations = goodsRelationService.selectByGoodsId(goodsId);
		
		String appCode = "";
		int amount = order.getUserNum();
		String expires = SignUtil.time2str(order.getOverTime(), "yyyy-MM-dd");
		String uporderUrl = "";
		if("0".equals(goods.getGoodsRalateGoods())) {
			for (GoodsRelation goodsRelation : goodsRelations) {
				appCode += goodsRelation.getProductNum()+",";
			}
			uporderUrl = read.getUpMealUrl();
		}else {
			appCode = goodsRelations.get(0).getProductNum();
			uporderUrl = read.getUpProductUrl();
		}
		
		String[] str = user.getCompanyDomainName().split("www.");
		String domain = str[1];
		String params = "domain="+domain+"&appCode="+appCode+"&amount="+amount+"&expires="+expires;
		
		uporderUrl += params;
		logger.info("uporderUrl:"+uporderUrl);
		
		long startTime = System.currentTimeMillis();
		logger.info("升级或者续费开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		
		String restr = HttpRequest.sendHttpRequest(uporderUrl,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("isReturn");
		long endTime = System.currentTimeMillis();
		logger.info("升级或者续费结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("升级或者续费调用接口时间毫秒数："+(endTime-startTime));
		logger.info(result);
		
		if("ok".equalsIgnoreCase(result) && "BST-OA001".equals(appCode)) {
			int userCount = amount + 3;
			updateOA(domain, user.getBelongCompanyName(), userCount, SignUtil.str2Date(expires, "yyyy-MM-dd"));
		}
		return result;
	}
	
	/**
	 * 退单
	 * @param domainStr 域名
	 */
	@SuppressWarnings("unchecked")
	public String chargeBack(String domainStr,String goodsName,Integer userId,int flag) throws Exception {
		String[] str = domainStr.split("www.");
		String domain = str[1];
		String chargeBackUrl = read.getChargeBackUrl()+"domain="+domain;
		logger.info("chargeBackUrl:"+chargeBackUrl);
		
		long startTime = System.currentTimeMillis();
		logger.info("退单开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		
		String restr = HttpRequest.sendHttpRequest(chargeBackUrl,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		
		logger.info(restr);
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("isReturn");
		long endTime = System.currentTimeMillis();
		logger.info("退单结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("退单调用接口时间毫秒数："+(endTime-startTime));
		logger.info("返回结果："+result);
		
		String delFriendUrl = read.getDelFirendUrl()+"tenantid="+domain;
		logger.info("delFriendUrl:"+delFriendUrl);
		String delFriendResult = HttpRequest.sendHttpRequest(delFriendUrl,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		logger.info(delFriendResult);
		
		//将删除的应用记录日志中
		OperateApply operateApply = new OperateApply();
		if(flag == 0) {
			operateApply.setContent("自动删除"+goodsName+"应用服务");
		}else if(flag == 1){
			operateApply.setContent("手动删除"+goodsName+"应用服务");
		}else {
			operateApply.setContent("手动删除作废的"+goodsName+"应用服务");
		}
		operateApply.setCreateTime(new Date());
		operateApply.setDomain(domain);
		operateApply.setUserId(userId);
		operateApply.setResult(result);
		operateApplyService.insertOperateApply(operateApply);
		return result;
	}
	
	/**
	 * 启用和停用整个公司
	 * @param domain 公司域名
	 * @param flag 启用和停用的标识（启用 ： true  停用：false）
	 */
	@SuppressWarnings("unchecked")
	public String doUser(String domain,boolean flag) throws Exception {
		String doUserUrl = read.getDoUserUrl()+"domain="+domain+"&flag="+flag;
		logger.info("doUserUrl:"+doUserUrl);
		
		long startTime = System.currentTimeMillis();
		logger.info("启用或者停用管理员账号开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		
		String restr = HttpRequest.sendHttpRequest(doUserUrl,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		
		logger.info(restr);
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("isReturn");
		long endTime = System.currentTimeMillis();
		logger.info("启用或者停用管理员账号结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("启用或者停用管理员账号调用接口时间毫秒数："+(endTime-startTime));
		logger.info("返回结果："+result);
		return result;
	}
	
	/**
	 * 启用和停用某个应用
	 * @param domain 公司域名
	 * @param flag 启用和停用的标识（启用 ： true  停用：false）
	 * @param appCodes 应用字符串，例如  “aaa,bbb,ccc,”
	 * @param goodsName 商品名称
	 */
	public void doProduct(String domainStr,boolean flag,String appCode,String goodsName) throws Exception {
		String[] str = domainStr.split("www.");
		String domain = str[1];
		String doProductUrl = read.getDoProductUrl()+"domain="+domain+"&flag="+flag+"&appCode="+appCode;
		logger.info("doProductUrl:"+doProductUrl);
		
		//long startTime = System.currentTimeMillis();
		logger.info("启用或者停用"+goodsName+"开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		
		String restr = HttpRequest.sendHttpRequest(doProductUrl,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		
		logger.info("返回结果："+restr);
		logger.info("启用或者停用"+goodsName+"结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		/*Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("isReturn");
		long endTime = System.currentTimeMillis();
		logger.info("启用或者停用"+goodsName+"结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("启用或者停用"+goodsName+"调用接口时间毫秒数："+(endTime-startTime));
		logger.info("返回结果："+result);
		return result;*/
	}
	
	/**
	 * 删除使用某应用的portal用户
	 * @param domain 公司域名
	 * @param appCodes 应用字符串，例如  “aaa,bbb,ccc,”
	 * @param goodsName 商品名称
	 */
	@SuppressWarnings("unchecked")
	public void deleteUsers(String domainStr,String appCode,String goodsName,Integer userId) throws Exception {
		String[] str = domainStr.split("www.");
		String domain = str[1];
		String deleteUsersUrl = read.getDeleteUsersUrl()+"domain="+domain+"&appCode="+appCode;
		logger.info("deleteUsersUrl:"+deleteUsersUrl);
		
		long startTime = System.currentTimeMillis();
		logger.info("清除使用"+goodsName+"的用户开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		
		String restr = HttpRequest.sendHttpRequest(deleteUsersUrl,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		
		logger.info("返回结果："+restr);
		//logger.info("清除使用"+goodsName+"的用户结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("isReturn");
		long endTime = System.currentTimeMillis();
		logger.info("清除使用"+goodsName+"的用户结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("清除使用"+goodsName+"的用户调用接口时间毫秒数："+(endTime-startTime));
		logger.info("返回结果："+result);
		
		//将删除的应用记录日志中
		OperateApply operateApply = new OperateApply();
		operateApply.setContent("删除"+goodsName+"应用服务");
		operateApply.setCreateTime(new Date());
		operateApply.setDomain(domain);
		operateApply.setUserId(userId);
		operateApply.setResult(result);
		operateApplyService.insertOperateApply(operateApply);
		//return result;
	}
	
	/**
	 * 自动部署的应用（OA、然之、CMS）开通
	 * @param domain 域名
	 * @param cname 公司名称
	 * @param users 用户数
	 * @param limidate 到期时间
	 * @param codeStr 企业编码和部门编码
	 * @param appCode 产品编码
	 * @return
	 */
	public boolean ktZZ(String domain,String cname,int users,Date limidate,String codeStr,String appCode) {
		String ip = oa.getOaIp(); //应用服务器IP
		String type = "oa"; //应用类型
		int port = oa.getOaServicePort(); //应用服务器端口
		if("BST-YUNXT001".equals(appCode)) {
			ip = oa.getRzIp();
			type = "yxt";
			port = oa.getRzServicePort();
		}else if("BST-CMS001".equals(appCode)) {
			ip = oa.getCmsIp();
			type = "cms";
			port = oa.getCmsServicePort();
		}
		
		String customer= "tomcat_" + domain.replace(".", "_"); //客户编号
		String databasename=type + domain.replace(".", "");//数据库名称
		String databaseusername=type + SignUtil.toHex();//数据库用户名
		String databasepassword=type + SignUtil.toHex();//数据库密码
//		String databaseusername=type + domain.replace(".", "");//数据库用户名
//		String databasepassword=type + domain.replace(".", "");//数据库密码
		
		Servers servers=new Servers();
		servers.setCorpid(customer);
		servers.setCname(cname);
		servers.setCorpname(oa.getCorpName());
		servers.setOrgs(NumberHelper.getIntValue(oa.getOrgs()));
		
		servers.setUsers(users);
		servers.setLimidate(limidate);
		servers.setMac(oa.getMac());
		servers.setDomain(domain);
		servers.setJavaoptions(codeStr);
		
		servers.setDatabasename(databasename);
		servers.setDatabasedialet(oa.getDatabaseDialet());
		servers.setDatabaseusername(databaseusername);
		servers.setDatabasepassword(databasepassword);
		servers.setDatabasemax(NumberHelper.getIntValue(oa.getDatabaseMax()));
		servers.setDatabasemin(NumberHelper.getIntValue(oa.getDatabaseMin()));
		servers.setDatabaseport(NumberHelper.getIntValue(oa.getDatabasePort()));
		servers.setWorkflowmodel(NumberHelper.getIntValue(oa.getWorkflowModel()));
		servers.setMobilenum(NumberHelper.getIntValue(oa.getMobileNum()));
		
		servers.setServerlinksmax(NumberHelper.getIntValue(oa.getServerLinksMax()));
		servers.setServerlinksmin(NumberHelper.getIntValue(oa.getServerLinksMin()));
		servers.setServerlinksfree(NumberHelper.getIntValue(oa.getServerLinksFree()));
		servers.setFileserverlinksmax(NumberHelper.getIntValue(oa.getServerLinksMax()));
		servers.setFileserverlinksmin(NumberHelper.getIntValue(oa.getFileServerLinksMin()));
		
		servers.setFileserverlinksfree(NumberHelper.getIntValue(oa.getFileServerLinksFree()));
		servers.setAttachmax(NumberHelper.getIntValue(oa.getAttachMax()));
		servers.setServermodel(oa.getServerModel());
		
		//服务
		TomcatClient t = new TomcatClient(new ConnectClient(ip,port),customer);
		try {
			t.connect();
			t.init_new(servers);//初始化服务
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				t.close();
			} catch (Exception e) {}
		}
		
		return false;
	}
	
	/**
	 * OA升级或者续费
	 * @param domain 域名
	 * @param cname 公司名称
	 * @param users 用户数
	 * @param limidate 到期时间
	 * @return
	 */
	public boolean updateOA(String domain,String cname,int users,Date limidate) {
		String customer= "tomcat_" + domain.replace(".", "_"); //客户编号
		
		Servers servers=new Servers();
		servers.setCorpid(customer);
		servers.setCname(cname);
		servers.setCorpname(oa.getCorpName());
		servers.setOrgs(NumberHelper.getIntValue(oa.getOrgs()));
		
		servers.setUsers(users);
		servers.setLimidate(limidate);
		servers.setMac(oa.getMac());
		servers.setDomain(domain);
		
		servers.setWorkflowmodel(NumberHelper.getIntValue(oa.getWorkflowModel()));
		servers.setServerlinksmax(NumberHelper.getIntValue(oa.getServerLinksMax()));
		servers.setFileserverlinksmax(NumberHelper.getIntValue(oa.getServerLinksMax()));
		servers.setAttachmax(NumberHelper.getIntValue(oa.getAttachMax()));
		
		//服务
		TomcatClient t = new TomcatClient(new ConnectClient(oa.getOaIp(),oa.getOaServicePort()),customer);
		try {
			t.connect();
			t.updateOA(servers);//更新OA
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				t.close();
			} catch (Exception e) {}
		}
		
		return false;
	}
	
	/*获取所有的产品信息*/
	public String getProducts() throws NumberFormatException, Exception {
		String getProUrl = read.getGetProductUrl();
		logger.info("getProUrl:"+getProUrl);
		
		long startTime = System.currentTimeMillis();
		logger.info("获取产品列表开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String restr = HttpRequest.sendHttpRequest(getProUrl,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		long endTime = System.currentTimeMillis();
		logger.info("获取产品列表结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("获取产品列表调用接口时间毫秒数："+(endTime-startTime));
		logger.info(restr);
		
		String str = restr.substring(restr.indexOf("["), restr.lastIndexOf("]")+1);
		logger.info(str);
		return str;
	}
	
	/*获取用户账单信息*/
	@SuppressWarnings("unchecked")
	public Map<String, Object> getBills(String caller,int pageSize,int pageIndex,String firstTime,String secondTime) throws NumberFormatException, Exception {
		String getBillUrl = read.getGetBillUrl()+"caller="+caller+"&pageSize="+pageSize+"&pageIndex="+pageIndex+"&starttime="+firstTime+"&stoptime="+secondTime;
		logger.info("getProUrl:"+getBillUrl);
		
		long startTime = System.currentTimeMillis();
		logger.info("获取用户账单开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
//		String restr = HttpRequest.sendHttpRequest(getBillUrl,read.getMcLocalhost(),8080);
		String restr = HttpRequest.sendHttpRequest(getBillUrl,read.getMcLocalhost(),Integer.valueOf(read.getEosPort()));
		long endTime = System.currentTimeMillis();
		logger.info("获取用户账单结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("获取用户账单调用接口时间毫秒数："+(endTime-startTime));
		logger.info(restr);
		
		String str = restr.substring(restr.indexOf("["), restr.lastIndexOf("]")+1);
		String s1 = restr.substring(restr.indexOf(":{")+1, restr.indexOf(",\"total\""));
		
		ObjectMapper objectMapper = new ObjectMapper();
		CallBill[] callBills = objectMapper.readValue(str, CallBill[].class);
		
		System.out.println(s1);
		Map<String, Object> map = objectMapper.readValue(s1, Map.class);
		int totalPage = (Integer)map.get("totalPage");
		int count = (Integer)map.get("count");
		/*for (CallBill callBill : callBills) {
			System.out.println(callBill.getCaller()+"--"+callBill.getStarttime()+"--"+callBill.getCalled()+"--"+callBill.getStoptime()+"--耗时:"+callBill.getSessiontime());
		}*/
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("callBills", callBills);
		resultMap.put("page", pageIndex);
		resultMap.put("totalPage", totalPage);
		resultMap.put("count", count);
		
		return resultMap;
	}
	
	/*获取虚拟号*/
	@SuppressWarnings("unchecked")
	public String getVn() throws NumberFormatException, Exception {
		String getVnUrl = read.getGetVnUrl();
		logger.info("getVnUrl:"+getVnUrl);
		
		long startTime = System.currentTimeMillis();
		logger.info("获取虚拟号开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String restr = HttpRequest.sendPost(getVnUrl, "");
		long endTime = System.currentTimeMillis();
		logger.info("获取虚拟号结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("获取虚拟号调用接口时间毫秒数："+(endTime-startTime));
		logger.info(restr);
		
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("result");
		return result;
	}
	
	/*回收虚拟号*/
	@SuppressWarnings("unchecked")
	public String recycleVn(String vn) throws NumberFormatException, Exception {
		String recycleVnUrl = read.getRecycleVnUrl();
		String params = "number="+vn;
		logger.info("recycleVnUrl:"+recycleVnUrl);
		logger.info("params:"+params);
		
		long startTime = System.currentTimeMillis();
		logger.info("回收虚拟号开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String restr = HttpRequest.sendPost(recycleVnUrl, params);
		long endTime = System.currentTimeMillis();
		logger.info("回收虚拟号结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("回收虚拟号调用接口时间毫秒数："+(endTime-startTime));
		logger.info(restr);
		
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("result");
		return result;
	}
	
	/*新增IP电话*/
	@SuppressWarnings("unchecked")
	public String addIp(String vn,String password) throws NumberFormatException, Exception {
		String addIpUrl = read.getAddIpUrl();
		String params = "vnumber="+vn+"&password="+password;
		logger.info("addIpUrl:"+addIpUrl);
		logger.info("params:"+params);
		
		long startTime = System.currentTimeMillis();
		logger.info("新增IP电话开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String restr = HttpRequest.sendPost(addIpUrl, params);
		long endTime = System.currentTimeMillis();
		logger.info("新增IP电话结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("新增IP电话调用接口时间毫秒数："+(endTime-startTime));
		logger.info(restr);
		
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("result");
		return result;
	}
	
	/*修改IP电话密码*/
	@SuppressWarnings("unchecked")
	public String updateIpPwd(String vn,String oldPwd,String newPwd) throws NumberFormatException, Exception {
		String updateIpPwdUrl = read.getUpdateIpPwdUrl();
		String params = "vnumber="+vn+"&oldpassword="+oldPwd+"&newpassword="+newPwd;
		logger.info("updateIpPwdUrl:"+updateIpPwdUrl);
		
		long startTime = System.currentTimeMillis();
		logger.info("修改IP电话密码开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String restr = HttpRequest.sendPost(updateIpPwdUrl, params);
		long endTime = System.currentTimeMillis();
		logger.info("修改IP电话密码结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("修改IP电话密码调用接口时间毫秒数："+(endTime-startTime));
		logger.info(restr);
		
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("result");
		return result;
	}
	
	/*删除IP电话*/
	@SuppressWarnings("unchecked")
	public String deleteIp(String vn) throws NumberFormatException, Exception {
		String deleteUrl = read.getDeleteIpUrl();
		String params = "vnumber="+vn;
		logger.info("deleteUrl:"+deleteUrl);
		logger.info("params:"+params);
		
		long startTime = System.currentTimeMillis();
		logger.info("删除IP电话开始时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		String restr = HttpRequest.sendPost(deleteUrl, params);
		long endTime = System.currentTimeMillis();
		logger.info("删除IP电话结束时间："+SignUtil.time2str(new Date(), "yyyy-MM-dd hh:mm:ss:SSS"));
		logger.info("删除IP电话调用接口时间毫秒数："+(endTime-startTime));
		logger.info(restr);
		
		Map<String, Object> returnMap = new ObjectMapper().readValue(restr, Map.class);
		String result = (String) returnMap.get("result");
		return result;
	}
	
	/*获取oa返回的消息*/
	public String  getOaMsg() {
//		return t.getMessage();
		return "";
	}
}
